/*
Copyright (C) 2003  Pierrick Brihaye
pierrick.brihaye@wanadoo.fr

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the
Free Software Foundation, Inc.
59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
or connect to:
http://www.fsf.org/copyleft/gpl.html
*/

package gpl.pierrick.brihaye.aramorph.lucene;


import gpl.pierrick.brihaye.aramorph.AraMorph;

import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;

import org.apache.lucene.analysis.CharTokenizer;
import org.apache.lucene.analysis.tokenattributes.TermAttribute;
import org.apache.lucene.util.AttributeSource;

/** A tokenizer that will return tokens in the arabic alphabet. This tokenizer
 * is a bit rude since it also filters digits and punctuation, even in an arabic
 * part of stream. Well... I've planned to write a
 * "universal", highly configurable, character tokenizer.
 * @author Ahmed ElDawy, 2009
 */
public class ArabicTokenizer extends CharTokenizer {

	public ArabicTokenizer(AttributeFactory factory, Reader input) {
		super(factory, input);
	}

	public ArabicTokenizer(AttributeSource source, Reader input) {
		super(source, input);
	}

	public ArabicTokenizer(Reader input) {
		super(input);
	}

	/** Whether or not a character is in the arabic alphabet.
	 * @param c The char
	 * @return The result
	 */
	protected boolean isTokenChar(char c) {
		return AraMorph.ARABIC_LETTERS.contains(new Integer(c));
	}
	
	public static void main(String[] args) {
//		try {
//			String arabicText = "بسم الله الرحمن الرحيم";
//			ArabicTokenizer at = new ArabicTokenizer(new StringReader(arabicText));
//			assert at.incrementToken();
//			assert at.getAttribute(TermAttribute.class).term().equals("بسم");
//			assert at.incrementToken();
//			assert at.getAttribute(TermAttribute.class).term().equals("الله");
//			assert at.incrementToken();
//			assert at.getAttribute(TermAttribute.class).term().equals("الرحمن");
//			assert at.incrementToken();
//			assert at.getAttribute(TermAttribute.class).term().equals("الرحيم");
//			assert !at.incrementToken();
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
	}
}
